#include "playsence.h"
#include "udpclient.h"
#include<thread>
#include<iostream>
using namespace std;


void PlaySence::Socket()
{
    char ip[] = "47.113.197.15";
    cli=new UDPClient(ip,54321);
    
    gamestart = true;

    function<void(void)> send = [&]() {
        while (true) {
            point s(m_hero.m_X, m_hero.m_Y);
            cli->Send(s);
            cli->SendData();
            Sleep(40);
        }
    };
    std::thread ths(send);

    //持续收数据
    while (true) {
        cli->ReceData();
        point r = cli->Rece();
        m_fri.m_X = r.x;
        m_fri.m_Y = r.y;
        m_fri.setposition(r.x, r.y);
        Sleep(40);
    }

    ths.join();
}

PlaySence::PlaySence(QWidget *parent) : QMainWindow(parent)
{
    gamestart = false;
    this->setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    this->setWindowTitle(GAME_TITLE);
    //定时器设置
    m_Timer.setInterval(GAME_RATE);
    this->playGame();
    m_recorder=0;
    m_midrecorder=0;
    //播放movie
    movie = new QMovie(this);
    movie->setFileName(WARINGMOVIE);
    movielab = new QLabel(this);
    movielab->setMovie(movie);
    movielab->move(100,300);
    movielab->resize(600,90);
    movielab->hide();
    //显示分数
    score="0";
    scorelab = new QLabel(this);
    QPalette pal;
    pal.setColor(QPalette::WindowText,Qt::white);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    scorelab->setFont(font);
    scorelab->setPalette(pal);
    scorelab->resize(100,50);
    scorelab->move(10,100);
    scorelab->setText("分数");
    //友军血量
    m_fri.m_boold = 3;
    m_hero.m_boold = 3;
    plane1lab = new QLabel(this);
    plane2lab = new QLabel(this);
    plane1lab->setFont(font);
    plane1lab->setPalette(pal);
    plane2lab->setFont(font);
    plane2lab->setPalette(pal);
    plane1lab->resize(160,50);
    plane1lab->move(10,160);
    plane2lab->resize(160,50);
    plane2lab->move(10,220);
    plane1lab->setText("我的血量：");
    plane2lab->setText("友军血量：");
    m_hero.wudi=true;
    m_fri.wudi=true;
    //boss血量
    bosslab = new QLabel(this);
    bosslab->setFont(font);
    bosslab->setPalette(pal);
    bosslab->resize(160,50);
    bosslab->move(10,280);
    bosslab->setText("boss血量：");
    bosslab->hide();
    bossblood="100";
    //初始化血量
    bossplane.m_boold=100;
    //boss战机初始化
    bossplane.m_Free=true;
    //初始化关卡时间信息
    Mid=false;
    End=false;
    Start=false;

    //
    QTimer::singleShot(5000,this,[=](){
        Start=true;
    });
    QTimer::singleShot(10000,this,[=](){
        Mid=true;
    });
    QTimer::singleShot(15000,this,[=](){
        Start=false;
        Mid=false;
        movielab->show();
        movie->start();
        QSound::play(WARNINGMUSIC);
    });
    QTimer::singleShot(18000,this,[=](){
        Mid=true;
        End=true;
        movie->stop();
        movielab->hide();
    });
}

void PlaySence::playGame()
{

    m_Timer.start();
    QSound::play(BACKGROUND);
    connect(&m_Timer,&QTimer::timeout,[=](){
        if((m_hero.m_boold>0||m_fri.m_boold>0)&& gamestart)
        {
            if(Start)
            {
                //敌军出场
                enemyToScene();
            }
            //地图滚动
            updatePosition();
            //重新更新图片
            update();
            //碰撞检测
            collisionDetection();
            //中等低级出场
            if(Mid)
            {
                midenemytosence();
            }
            if(End)
            {
                bossplane.m_Free=false;
                bosslab->show();
            }
        }
        else
        {

        }
    });

}

void PlaySence::updatePosition()
{
    m_map.mapPosition();
    //发射子弹
    if(m_hero.m_boold>0)
        m_hero.shoot();
    if(m_fri.m_boold>0)
        m_fri.shoot();
    for(int i=0;i<BULLET_NUM;i++)
    {
        //若子弹状态为非空闲则更新子弹位置
        if(!m_hero.m_bullet[i].m_Free)
        {
            m_hero.m_bullet[i].updatePosition();
        }
    }
    //友军子弹
    for(int i=0;i<BULLET_NUM;i++)
    {
        if(!m_fri.m_bullet[i].m_Free)
        {
            m_fri.m_bullet[i].updatePosition();
        }
    }
    //敌军坐标计算
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(!m_enemys[i].m_Free)
        {
            m_enemys[i].updatePosition();
        }
    }
    //boss
    if(End&&bossplane.m_boold>0)
    {
        bossplane.movenewposition();
        bossplane.shoot();
        for(int i=0;i<BULLET_NUM;i++)
        {
            if(!bossplane.m_bullet[i].m_Free)
            {
                bossplane.m_bullet[i].updatePositionBoss();
            }
        }
    }
    //中等战机坐标计算
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(!m_midenemys[i].m_Free)
        {
            m_midenemys[i].updatePosition();
        }
    }
    //计算爆炸播放的图片
    for(int  i  =  0  ;  i  <  BOMB_NUM;i++)
    {
    if(m_bombs[i].m_Free  ==  false)
    {
    m_bombs[i].updateInfo();
    }
    }

}

void PlaySence::paintEvent(QPaintEvent *event)
{
    QPainter paint(this);
    //地图
    paint.drawPixmap(0,m_map.m_map1_posY,m_map.m_map1);
    paint.drawPixmap(0,m_map.m_map2_posY,m_map.m_map2);
    //我方战机
    if(m_hero.m_boold>0)
    {
        paint.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);
        //子弹
        for(int i=0;i<BULLET_NUM;i++)
        {
            if(!m_hero.m_bullet[i].m_Free)
            {
                paint.drawPixmap(m_hero.m_bullet[i].m_X,m_hero.m_bullet[i].m_Y,m_hero.m_bullet[i].m_Bullet);
            }
        }
    }
    if(m_fri.m_boold>0)
    {
        paint.drawPixmap(m_fri.m_X,m_fri.m_Y,m_fri.m_friplane);
        //友军子弹
        for(int i=0;i<BULLET_NUM;i++)
        {
            if(!m_fri.m_bullet[i].m_Free)
            {
                paint.drawPixmap(m_fri.m_bullet[i].m_X,m_fri.m_bullet[i].m_Y,m_fri.m_bullet[i].m_Bullet);
            }
        }
    }
    //敌机
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(!m_enemys[i].m_Free)
        {
            paint.drawPixmap(m_enemys[i].m_X,m_enemys[i].m_Y,m_enemys[i].m_enemy);
        }
    }
    //中等敌机
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(!m_midenemys[i].m_Free)
        {
            paint.drawPixmap(m_midenemys[i].m_X,m_midenemys[i].m_Y,m_midenemys[i].m_midenemy);
        }
    }
    //绘制爆炸图片
    for(int  i  =  0  ;  i  <  BOMB_NUM;i++)
    {
    if(m_bombs[i].m_Free  ==  false)
    {

    paint.drawPixmap(m_bombs[i].m_X,m_bombs[i].m_Y,m_bombs[i].m_pixArr[m_bombs[i].m_index]);
    }
    }
    //显示分数
    paint.setPen(Qt::white);
    paint.setFont(QFont("华文新魏",15));
    paint.drawText(120,136,score);
    //显示血量
    QPixmap pix;
    pix.load(PLANE_BLOOD);
    if(m_hero.m_boold==3)
    {
        paint.drawPixmap(180,191,pix);
        paint.drawPixmap(220,191,pix);
        paint.drawPixmap(260,191,pix);
    }
    else if(m_hero.m_boold==2)
    {
        paint.drawPixmap(180,191,pix);
        paint.drawPixmap(220,191,pix);
    }
    else if(m_hero.m_boold==1)
    {
        paint.drawPixmap(180,191,pix);
    }
    else
    {
        paint.drawText(180,191,"阵亡");
    }
    if(m_fri.m_boold==3)
    {
        paint.drawPixmap(180,261,pix);
        paint.drawPixmap(220,261,pix);
        paint.drawPixmap(260,261,pix);
    }
    else if(m_fri.m_boold==2)
    {
        paint.drawPixmap(180,261,pix);
        paint.drawPixmap(220,261,pix);
    }
    else if(m_fri.m_boold==1)
    {
        paint.drawPixmap(180,261,pix);
    }
    else
    {
        paint.drawText(180,261,"阵亡");
    }
    //boss子弹
    if(bossplane.m_boold>0&&End)
    {
        paint.drawPixmap(bossplane.m_X,bossplane.m_Y,bossplane.m_BossPlane);
        for(int i=0;i<BULLET_NUM;i++)
        {
            if(!bossplane.m_bullet[i].m_Free)
            {
                paint.drawPixmap(bossplane.m_bullet[i].m_X,bossplane.m_bullet[i].m_Y,bossplane.m_bullet[i].m_Bulletboss);
            }
        }
        paint.setPen(Qt::white);
        paint.setFont(QFont("华文新魏",15));
        paint.drawText(180,320,bossblood);
    }
}

void PlaySence::keyPressEvent(QKeyEvent *event)
{
    m_hero.m_X=m_hero.m_X-m_hero.m_X%step;
    m_hero.m_Y=m_hero.m_Y-m_hero.m_Y%step;
    if(event->key()==Qt::Key_Left)
    {
        m_hero.m_X=(m_hero.m_X-step)<0?0:m_hero.m_X-step;
        m_hero.setPosition(m_hero.m_X,m_hero.m_Y);
    }
    if(event->key()==Qt::Key_Right)
    {
        m_hero.m_X=(m_hero.m_X+step)>GAME_WIDTH-m_hero.m_Plane.width()?GAME_WIDTH-m_hero.m_Plane.width():m_hero.m_X+step;
        m_hero.setPosition(m_hero.m_X,m_hero.m_Y);
    }
    if(event->key()==Qt::Key_Up)
    {
        m_hero.m_Y=(m_hero.m_Y-step)<0?0:m_hero.m_Y-step;
        m_hero.setPosition(m_hero.m_X,m_hero.m_Y);
    }
    if(event->key()==Qt::Key_Down)
    {
        m_hero.m_Y=(m_hero.m_Y+step)>GAME_HEIGHT-m_hero.m_Plane.height()?GAME_HEIGHT-m_hero.m_Plane.height():m_hero.m_Y+step;
        m_hero.setPosition(m_hero.m_X,m_hero.m_Y);
    }
}

void PlaySence::enemyToScene()
{
    m_recorder++;
    if(m_recorder<ENEMY_INTERVAL)
    {
        return;
    }
    m_recorder=0;
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free)
        {
            m_enemys[i].m_Free=false;
            m_enemys[i].m_X  =  rand()  %  (GAME_WIDTH  -  m_enemys[i].m_Rect.width());
            m_enemys[i].m_Y  =  -m_enemys[i].m_Rect.height();
            break;
        }
    }
}

void PlaySence::midenemytosence()
{
    m_midrecorder++;
    if(m_midrecorder<ENEMY_INTERVAL)
    {
        return;
    }
    m_midrecorder=0;
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_midenemys[i].m_Free)
        {
            m_midenemys[i].m_Free=false;
            m_midenemys[i].m_X  =  rand()  %  (GAME_WIDTH  -  m_midenemys[i].m_Rect.width());
            m_midenemys[i].m_Y  =  -m_midenemys[i].m_Rect.height();
            break;
        }
    }
}

void PlaySence::collisionDetection()
{
    int temp;
    //普通战机碰撞判断
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_enemys[i].m_Free)
        {
            continue;
        }
        for(int j=0;j<BULLET_NUM;j++)
        {
            if(m_hero.m_bullet[i].m_Free)
            {
                continue;
            }
            if(m_enemys[i].m_Rect.intersects(m_hero.m_bullet[j].m_Rect))
            {
                m_enemys[i].m_Free  =  true;
                m_hero.m_bullet[j].m_Free  =  true;
                temp = score.toInt();
                temp++;
                score=QString::number(temp);
                //爆炸音效
                QSound::play(BAOZHA);
                //播放爆炸效果
                for(int  k  =  0  ;  k  <  BOMB_NUM;k++)
                {
                if(m_bombs[k].m_Free)
                {
                //爆炸状态设置为非空闲
                    m_bombs[k].m_Free  =  false;
                    //更新坐标

                    m_bombs[k].m_X  =  m_enemys[i].m_X;
                    m_bombs[k].m_Y  =  m_enemys[i].m_Y;
                    break;
                    }
                }
            }
        }
        for(int k=0;k<BULLET_NUM;k++)
        {
            if(m_fri.m_bullet[i].m_Free)
            {
                continue;
            }
            if(m_enemys[i].m_Rect.intersects(m_fri.m_bullet[k].m_Rect))
            {
                m_enemys[i].m_Free = true;
                m_fri.m_bullet[k].m_Free = true;
                temp = score.toInt();
                temp++;
                score=QString::number(temp);
                //播放爆炸效果
//                QSound::play(BAOZHA);
                for(int  l  =  0  ;  l  <  BOMB_NUM;l++)
                {
                if(m_bombs[l].m_Free)
                {
                //爆炸状态设置为非空闲
                    m_bombs[l].m_Free  =  false;
                    //更新坐标

                    m_bombs[l].m_X  =  m_enemys[i].m_X;
                    m_bombs[l].m_Y  =  m_enemys[i].m_Y;
                    break;
                    }
                }
            }
        }
        if(m_enemys[i].m_Rect.intersects(m_hero.m_Rect)&&!m_enemys[i].m_Free&&m_hero.wudi)
        {
            m_enemys[i].m_Free=true;
            m_hero.m_boold--;
            m_hero.wudi=false;
            QTimer::singleShot(2000,this,[=](){
                m_hero.wudi=true;
            });
        }
        if(m_enemys[i].m_Rect.intersects(m_fri.m_Rect)&&!m_enemys[i].m_Free&&m_fri.wudi)
        {
            m_enemys[i].m_Free=true;
            m_fri.m_boold--;
            m_fri.wudi=false;
            QTimer::singleShot(2000,this,[=](){
                m_fri.wudi=true;
            });
        }
    }
    //中等战机判断
    for(int i=0;i<ENEMY_NUM;i++)
    {
        if(m_midenemys[i].m_Free)
        {
            continue;
        }
        for(int j=0;j<BULLET_NUM;j++)
        {
            if(m_hero.m_bullet[i].m_Free)
            {
                continue;
            }
            if(m_midenemys[i].m_Rect.intersects(m_hero.m_bullet[j].m_Rect))
            {
                m_midenemys[i].Blood--;
                if(m_midenemys[i].Blood<1)
                {
                    QSound::play(BAOZHA);
                    m_midenemys[i].m_Free=true;
                    m_midenemys[i].Blood=10;
                    temp = score.toInt();
                    temp+=5;
                    score=QString::number(temp);
                }
                m_hero.m_bullet[j].m_Free  =  true;
                //播放爆炸效果
                for(int  k  =  0  ;  k  <  BOMB_NUM;k++)
                {
                if(m_bombs[k].m_Free)
                {
                //爆炸状态设置为非空闲
                    m_bombs[k].m_Free  =  false;
                    //更新坐标

                    m_bombs[k].m_X  =  m_midenemys[i].m_X;
                    m_bombs[k].m_Y  =  m_midenemys[i].m_Y;
                    break;
                    }
                }
            }
        }
        for(int k=0;k<BULLET_NUM;k++)
        {
            if(m_fri.m_bullet[i].m_Free)
            {
                continue;
            }
            if(m_midenemys[i].m_Rect.intersects(m_fri.m_bullet[k].m_Rect))
            {
                m_midenemys[i].Blood--;
                if(m_midenemys[i].Blood<1)
                {
                    QSound::play(BAOZHA);
                    m_midenemys[i].m_Free=true;
                    m_midenemys[i].Blood=10;
                    temp = score.toInt();
                    temp+=5;
                    score=QString::number(temp);
                }
                m_fri.m_bullet[k].m_Free = true;
                //播放爆炸效果
                for(int  l  =  0  ;  l  <  BOMB_NUM;l++)
                {
                if(m_bombs[l].m_Free)
                {
                //爆炸状态设置为非空闲
                    m_bombs[l].m_Free  =  false;
                    //更新坐标

                    m_bombs[l].m_X  =  m_midenemys[i].m_X;
                    m_bombs[l].m_Y  =  m_midenemys[i].m_Y;
                    break;
                    }
                }
            }
        }
        if(m_midenemys[i].m_Rect.intersects(m_hero.m_Rect)&&!m_midenemys[i].m_Free&&m_hero.wudi)
        {
            m_midenemys[i].m_Free=true;
            m_hero.m_boold--;
            m_hero.wudi=false;
            temp = plane1.toInt();
            temp--;
            plane1 = QString::number(temp);
            QTimer::singleShot(2000,this,[=](){
                m_hero.wudi=true;
            });
        }
        if(m_midenemys[i].m_Rect.intersects(m_fri.m_Rect)&&!m_midenemys[i].m_Free&&m_fri.wudi)
        {
            m_midenemys[i].m_Free=true;
            m_fri.m_boold--;
            m_fri.wudi=false;
            temp = plane2.toInt();
            temp--;
            plane2 = QString::number(temp);
            QTimer::singleShot(2000,this,[=](){
                m_fri.wudi=true;
            });
        }
    }
    //boss判断
    for(int i=0;i<BULLET_NUM;i++)
    {
        if(m_hero.m_bullet[i].m_Free)
        {
            continue;
        }
        if(bossplane.m_Rect.intersects(m_hero.m_bullet[i].m_Rect)&&End)
        {
            if(bossplane.m_boold<1)
            {
                bossplane.m_boold=0;
                bossblood=QString::number(bossplane.m_boold);
            }
            else
            {
                 bossplane.m_boold--;
                 bossblood=QString::number(bossplane.m_boold);
            }
            m_hero.m_bullet[i].m_Free  =  true;
            //播放爆炸效果
            for(int  k  =  0  ;  k  <  BOMB_NUM;k++)
            {
            if(m_bombs[k].m_Free)
            {
            //爆炸状态设置为非空闲
                m_bombs[k].m_Free  =  false;
                //更新坐标

                m_bombs[k].m_X  =  m_midenemys[i].m_X;
                m_bombs[k].m_Y  =  m_midenemys[i].m_Y;
                break;
            }
            }
        }
    }
    for(int i=0;i<BULLET_NUM;i++)
    {
        if(m_fri.m_bullet[i].m_Free)
        {
            continue;
        }
        if(bossplane.m_Rect.intersects(m_fri.m_bullet[i].m_Rect)&&End)
        {
            if(bossplane.m_boold<1)
            {
                bossplane.m_boold=0;
                bossblood=QString::number(bossplane.m_boold);
            }
            else
            {
                 bossplane.m_boold--;
                 bossblood=QString::number(bossplane.m_boold);
            }
            m_fri.m_bullet[i].m_Free = true;
            //播放爆炸效果
            for(int  l  =  0  ;  l  <  BOMB_NUM;l++)
            {
            if(m_bombs[l].m_Free)
            {
            //爆炸状态设置为非空闲
                m_bombs[l].m_Free  =  false;
                //更新坐标

                m_bombs[l].m_X  =  m_midenemys[i].m_X;
                m_bombs[l].m_Y  =  m_midenemys[i].m_Y;
                break;
                }
            }
        }
    }
    for(int i=0;i<BULLET_NUM;i++)
    {
        if(bossplane.m_bullet[i].m_Free)
        {
            return;
        }
        if(bossplane.m_bullet[i].m_Rect.intersects(m_hero.m_Rect)&&!bossplane.m_bullet[i].m_Free&&m_hero.wudi)
        {
            bossplane.m_bullet[i].m_Free=true;
            m_hero.m_boold--;
            m_hero.wudi=false;
            QTimer::singleShot(2000,this,[=](){
                m_hero.wudi=true;
            });
        }
        if(bossplane.m_bullet[i].m_Rect.intersects(m_fri.m_Rect)&&!bossplane.m_bullet[i].m_Free&&m_fri.wudi)
        {
            bossplane.m_bullet[i].m_Free=true;
            m_fri.m_boold--;
            m_fri.wudi=false;
            QTimer::singleShot(2000,this,[=](){
                m_fri.wudi=true;
            });
        }
    }
}
